package com.zhonghui.modlules.user.controller;

import cn.hutool.crypto.SecureUtil;
import com.google.common.collect.Maps;
import com.zhonghui.common.bean.LocalUser;
import com.zhonghui.common.eshopConstant.ShopConstants;
import com.zhonghui.common.eshopConstant.SystemConfigConstants;
import com.zhonghui.common.eshopDomain.ShopUser;
import com.zhonghui.common.eshopEnum.BillInfoEnum;
import com.zhonghui.common.eshopUtil.ApiResult;
import com.zhonghui.modlules.shop.eshopService.SystemConfigService;
import com.zhonghui.modlules.shop.eshopService.SystemGroupDataService;
import com.zhonghui.modlules.user.eshopService.UserBillService;
import com.zhonghui.modlules.user.eshopService.UserService;
import com.zhonghui.modlules.user.eshopService.UserSignService;
import com.zhonghui.modlules.user.eshopVo.SignVo;
import com.zhonghui.modlules.user.eshopVo.UserQueryVo;
import com.zhonghui.modlules.user.param.UserPasswordParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "用户中心", tags = "用户:用户中心", description = "用户中心")
public class UserController {
// 细节
    private final UserService userService;
    private final SystemConfigService systemConfigService;
    private final SystemGroupDataService systemGroupDataService;

    private final UserSignService signService;
    private final UserBillService billService;


    @ApiOperation(value = "用户资金统计",notes = "用户资金统计")
    @GetMapping("/user/balance")
    public ApiResult<Object> balance(){
        ShopUser shopUser = LocalUser.getUser();
        Map<String, Object> map = Maps.newHashMap();
        Double[] userMoney = userService.getUserMoney(shopUser.getUid());
        map.put("nowMoney",shopUser.getNowMoney());
        map.put("orderStatusSum",userMoney[0]);
        map.put("recharge",userMoney[1]);
        map.put("is_hide",systemConfigService.getData(SystemConfigConstants.YSHOP_SHOW_RECHARGE));
        return ApiResult.ok(map);
    }

    @ApiOperation(value = "获取个人中心菜单",notes = "获取个人中心菜单")
    @GetMapping("/user/menus")
    public ApiResult<Map<String,Object>> userMenus(){
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("routine_my_menus",systemGroupDataService.getDatas(ShopConstants.YSHOP_MY_MENUES));
        return ApiResult.ok(map);
    }

    @ApiOperation(value = "用户修改密码",notes = "用户修改密码")
    @PostMapping("/user/password")
    public ApiResult<String> userPassword(@Validated @RequestParam UserPasswordParam userPasswordParam){
        ShopUser shopUser = LocalUser.getUser();
        String password = shopUser.getPassword();
        if (!password.equals(SecureUtil.md5(userPasswordParam.getPassword()))){
            return ApiResult.fail("原密码错误，请重试");
        }

        shopUser.setPassword(SecureUtil.md5(userPasswordParam.getNewPassword()));
        userService.save(shopUser);
        return ApiResult.ok("修改成功");
    }

    @ApiOperation(value = "签到用户信息",notes = "签到用户信息")
    @PostMapping("/sign/info")
    public ApiResult<UserQueryVo> signInfo(){
        ShopUser shopUser = LocalUser.getUser();
        return ApiResult.ok(signService.userSignInfo(shopUser));
    }

    @ApiOperation(value = "签到配置",notes = "签到配置")
    @GetMapping("/sign/group")
    public ApiResult<Object> signGroup(){
        return ApiResult.ok(systemGroupDataService.getDatas(ShopConstants.YSHOP_SIGN_DAY_NUM));
    }

    @ApiOperation(value = "签到列表",notes = "签到列表")
    @GetMapping("/sign/list")
    public ApiResult<List<SignVo>> signList(@RequestParam(value = "page",defaultValue = "1")int page,
                                            @RequestParam(value = "limit",defaultValue = "10") int limit){
        Long uid = LocalUser.getUser().getUid();
        return ApiResult.ok(signService.getSignList(uid,page,limit));
    }

    @ApiOperation(value = "签到列表（年月）",notes = "签到列表（年月）")
    @GetMapping("/sign/bill/list")
    public ApiResult<Object> signBill(@RequestParam(value = "page",defaultValue = "1")int page,
                                      @RequestParam(value = "limit",defaultValue = "10") int limit){
        Long uid = LocalUser.getUser().getUid();
        return ApiResult.ok(billService.getUserBillList(page,limit,uid, BillInfoEnum.SIGN_INTEGRAL.getValue()));
    }

    @ApiOperation(value = "开始签到",notes = "开始签到")
    @PostMapping("/sign")
    public ApiResult<Object> sign(){
        ShopUser shopUser = LocalUser.getUser();
        int integral = signService.sign(shopUser);
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("integral",integral);
        return ApiResult.ok(map,"签到获得"+integral+"积分");
    }
}
